﻿<%@ Page Title="Generate Macro" Language="C#" MasterPageFile="~/MasterPage.master"
     AutoEventWireup="true" CodeFile="Generate.aspx.cs" Inherits="Generate"
     ValidateRequest="false" %>

<%-- Generate New Macro --%>
<asp:Content ID="Content1" ContentPlaceHolderID="CphMaster" runat="Server">
     <h1 class="pagetitlesystem">
          <asp:Literal ID="lblTitle" runat="server" meta:resourcekey="lblTitleResource1"
               Text="Generate Macro" /></h1>
     <asp:UpdatePanel runat="server" ID="updpanSelectionTables">
          <ContentTemplate>
               <asp:Label runat="server" Text="Experiment" Width="150" />
               <asp:Label runat="server" ID="lblExperimentName" Width="500"
                    ForeColor="Black" Text="Initial Text" />
               <br />
               <br />
               <asp:Label runat="server" Text="Sample" Width="150" />
               <asp:LinkButton runat="server" Text="Select Sample" CausesValidation="False"
                    ID="lbSelectSample" OnClick="lbSelectSample_Click" />
               <br />
               <br />
               <asp:Label runat="server" Text="Spectrometer" Width="150" />
               <asp:LinkButton runat="server" Text="Select Spectrometer" CausesValidation="false"
                    ID="lbSelectSpectrometer" OnClick="lbSelectSpectrometer_Click" />
               <br />
               <br />
               <asp:Label runat="server" Text="Remark" Width="150" Style="vertical-align: top"></asp:Label>
               <asp:TextBox runat="server" Width="500" MaxLength="2000" ID="tbRemark"
                    TextMode="MultiLine" Rows="5"></asp:TextBox>
               <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="tbRemark"
                    Style="vertical-align: top" EnableClientScript="False" ErrorMessage="Scripts Are Not Allowed."
                    OnServerValidate="ValidateForScripts" ValidateEmptyText="True"
                    Display="Dynamic"></asp:CustomValidator>
               <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                    Style="vertical-align: top" ControlToValidate="tbRemark"
                    Display="Dynamic" ErrorMessage="Content must present."></asp:RequiredFieldValidator>
               <br />
               <br />
               <asp:Button runat="server" Text="Generate" ID="ButGenerate" OnClick="ButGenerate_Click" />
               <asp:Label ID="LinkLabel" runat="server" Text="Label" Visible="False"></asp:Label>
               <asp:HyperLink runat="server" ID="LinkToDownloadMacro" Text="Download"
                    NavigateUrl="~/GetMacro.aspx?File=" Visible="false"></asp:HyperLink>
               <asp:ScriptManager ID="ScriptManager1" runat="server">
               </asp:ScriptManager>
               <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="updpanSelectionTables"
                    DisplayAfter="300">
                    <ProgressTemplate>
                         <div class="PleaseWait">
                              Please Wait...</div>
                    </ProgressTemplate>
               </asp:UpdateProgress>
               <asp:Panel runat="server" ID="panSelectionTables" Visible="false">
                    <hr />
                    <asp:MultiView ID="mvSelectionTables" runat="server" ActiveViewIndex="0">
                         <asp:View ID="View1" runat="server">
                              <asp:Panel runat="server">
                                   <asp:DropDownList ID="drlstSelectSampleName" runat="server" Width="150"
                                        DataSourceID="SampleNamesDataSource" DataTextField="name"
                                        DataValueField="name" AutoPostBack="true" AppendDataBoundItems="true">
                                        <asp:ListItem Enabled="true" Text="Please Select" Selected="True"
                                             Value="none"></asp:ListItem>
                                   </asp:DropDownList>
                                   <asp:SqlDataSource ID="SampleNamesDataSource" runat="server"
                                        ConnectionString="<%$ ConnectionStrings:NMRUserDataConnectionString %>"
                                        SelectCommand="SELECT samples.name FROM relation_samples_to_users INNER JOIN samples ON relation_samples_to_users.sampleid = samples.id WHERE (relation_samples_to_users.username = @username) GROUP BY samples.name">
                                        <SelectParameters>
                                             <asp:Parameter DefaultValue="none" Name="username" />
                                        </SelectParameters>
                                   </asp:SqlDataSource>
                                   <asp:LinkButton runat="server" CssClass="minorlink" ID="RefreshButton1"
                                        OnClick="RefreshButton_Click" CausesValidation="false">Refresh</asp:LinkButton>
                              </asp:Panel>
                              <br />
                              <asp:GridView ID="SamplesTableGridView" runat="server" AllowPaging="True"
                                   AllowSorting="True" AutoGenerateColumns="False" BackColor="White"
                                   BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
                                   DataKeyNames="Id, Version" CellPadding="3" DataSourceID="SamplesDataSource"
                                   GridLines="Horizontal" OnRowCommand="SamplesTableGv_RowCommand"
                                   Width="100%" EmptyDataText="Please select the sample.">
                                   <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                                   <Columns>
                                        <asp:ButtonField ButtonType="Link" DataTextField="Version" HeaderText="Ver"
                                             SortExpression="Version" CausesValidation="false" CommandName="SelectSample"
                                             ShowHeader="True">
                                             <ItemStyle Width="10px" VerticalAlign="Top" Font-Underline="true" />
                                        </asp:ButtonField>
                                        <asp:BoundField DataField="Buffer" HeaderText="Buffer" SortExpression="Buffer">
                                             <ItemStyle Width="150px" VerticalAlign="Top" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="Labeling" HeaderText="Labeling" SortExpression="Labeling">
                                             <ItemStyle Width="100px" VerticalAlign="Top" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="Short Description" HeaderText="Short Description"
                                             SortExpression="Short Description">
                                             <ItemStyle VerticalAlign="Top" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="Info" HeaderText="Version Info" SortExpression="Info">
                                             <ItemStyle Width="200px" VerticalAlign="Top" />
                                        </asp:BoundField>
                                        <asp:TemplateField HeaderText="Contact" SortExpression="Contact">
                                             <ItemTemplate>
                                                  <asp:HyperLink runat="server" Text='<%# Eval("Contact") %>' NavigateUrl='<%# Eval("Email", "mailto://{0}") %>'
                                                       ID="hlEmail" />
                                             </ItemTemplate>
                                             <ItemStyle Width="70px" VerticalAlign="Top" Font-Underline="true" />
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="Datum" DataFormatString="{0:d}" HeaderText="Datum"
                                             SortExpression="Datum">
                                             <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Width="100px" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False"
                                             SortExpression="Id" Visible="False" />
                                        <asp:BoundField DataField="Version" HeaderText="Version" />
                                   </Columns>
                                   <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                                   <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                                   <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                                   <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7"
                                        HorizontalAlign="Left" />
                                   <AlternatingRowStyle BackColor="#F7F7F7" />
                              </asp:GridView>
                              <asp:SqlDataSource ID="SamplesDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:NMRUserDataConnectionString %>"
                                   SelectCommand="SELECT s1.id, s1.name AS Name, s1.buffer AS Buffer, s1.labeling AS Labeling, s1.memo AS [Short Description], s1.contact AS Contact, s1.contactmail AS Email, s1.date AS Datum, s1.version AS Version, s1.versioninfo AS Info FROM samples AS s1 INNER JOIN relation_samples_to_users AS rsu ON s1.id = rsu.sampleid WHERE (rsu.username = @username) AND (s1.name = @name) ORDER BY Version DESC">
                                   <SelectParameters>
                                        <asp:Parameter Name="username" Type="String" DefaultValue="none" />
                                        <asp:ControlParameter ControlID="drlstSelectSampleName" Name="name"
                                             DefaultValue="none" />
                                   </SelectParameters>
                              </asp:SqlDataSource>
                         </asp:View>
                         <asp:View ID="View2" runat="server">
                              <asp:LinkButton runat="server" CssClass="minorlink" ID="RefreshButton2"
                                   OnClick="RefreshButton_Click" CausesValidation="false">Refresh</asp:LinkButton>
                              <asp:GridView ID="SpectrometersTableGv" runat="server" AllowSorting="True"
                                   AutoGenerateColumns="False" DataSourceID="SpectrometersDataSource"
                                   EmptyDataText="There is no Spectrometers" DataKeyNames="Id, Name"
                                   Width="100%" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
                                   BorderWidth="1px" CellPadding="3" GridLines="Horizontal"
                                   OnRowCommand="SpectrometersTableGv_RowCommand">
                                   <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                                   <Columns>
                                        <asp:ButtonField ButtonType="Link" DataTextField="Name" HeaderText="Name"
                                             SortExpression="Name" CausesValidation="false" CommandName="SelectSpectrometer"
                                             ShowHeader="True">
                                             <ItemStyle Width="100px" VerticalAlign="Top" Font-Underline="true" />
                                        </asp:ButtonField>
                                        <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location"
                                             ItemStyle-Width="200px">
                                             <ItemStyle Width="200px" VerticalAlign="Top" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="Short Description" HeaderText="Short Description"
                                             SortExpression="Short Description">
                                             <ItemStyle VerticalAlign="Top" />
                                        </asp:BoundField>
                                        <asp:TemplateField HeaderText="Contact" SortExpression="Contact">
                                             <ItemTemplate>
                                                  <asp:HyperLink runat="server" Text='<%# Eval("Contact") %>' NavigateUrl='<%# Eval("Email", "mailto://{0}") %>'
                                                       ID="hlEmail" />
                                             </ItemTemplate>
                                             <ItemStyle Width="200px" VerticalAlign="Top" Font-Underline="true" />
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date"
                                             ItemStyle-Width="100px" DataFormatString="{0:d}">
                                             <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" Width="100px" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False"
                                             SortExpression="Id" Visible="False" />
                                        <asp:BoundField DataField="Name" HeaderText="Temp" Visible="false" />
                                        <asp:CommandField />
                                   </Columns>
                                   <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                                   <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                                   <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                                   <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7"
                                        HorizontalAlign="Left" />
                                   <AlternatingRowStyle BackColor="#F7F7F7" />
                              </asp:GridView>
                              <asp:SqlDataSource ID="SpectrometersDataSource" runat="server"
                                   ConnectionString="<%$ ConnectionStrings:NMRUserDataConnectionString %>"
                                   SelectCommand="SELECT
	s1.id AS Id,
	s1.[date] AS [Date],
	s1.name AS Name,
	s1.memo AS [Short Description],
	s1.location AS Location,
	s1.contact AS Contact,
	s1.contactmail AS Email,
	s1.[version] AS [Version]
FROM (
	SELECT 
		s2.name, 
		MAX(s2.[version]) AS MaxVersion 
	FROM 
		spectrometers AS s2 
	INNER JOIN relation_spectrometers_to_users AS rsu 
		ON s2.id = rsu.spectrometerid 
	WHERE (rsu.username = @username) 
	GROUP BY s2.name) AS x
INNER JOIN spectrometers AS s1
	ON s1.name = x.name AND s1.[version]=x.MaxVersion
INNER JOIN relation_spectrometers_to_users AS rsu
	ON s1.id=rsu.spectrometerid
WHERE (rsu.username = @username)
	">
                                   <SelectParameters>
                                        <asp:Parameter Name="username" DefaultValue="none" />
                                   </SelectParameters>
                              </asp:SqlDataSource>
                         </asp:View>
                    </asp:MultiView>
               </asp:Panel>
          </ContentTemplate>
          <Triggers>
               <asp:AsyncPostBackTrigger ControlID="lbSelectSample" EventName="Click" />
               <asp:AsyncPostBackTrigger ControlID="lbSelectSpectrometer" EventName="Click" />
               <asp:AsyncPostBackTrigger ControlID="RefreshButton1" EventName="Click" />
               <asp:AsyncPostBackTrigger ControlID="RefreshButton2" EventName="Click" />
          </Triggers>
     </asp:UpdatePanel>
</asp:Content>
